import axios from "axios";
import methods from "methods";
import "../config";

axios.defaults.baseURL = window.env === "prod" ? window.prodBaseUrl : window.devBaseUrl;
axios.defaults.timeout = 120000;


export const apiPostFetch = (url, params = {}) => {
  return new Promise((resolve, reject) => {

    let u = url.startsWith("http") ? url : axios.defaults.baseURL + url;
    let access_token = methods.getOAuth2token();

    api.ajax({
      url: u,
      headers: {
        "Content-Type": "application/json;charset=utf-8",
      },
      method: "post",
      dataType: "json",
      data: {
        body: {
          access_token,
          ...params,
        },
      },
    }, (ret, err) => {
      if (ret) {
        if (ret.status) {
          resolve(ret.data);
        } else {
          ret.netError = 0;
          reject(ret);
        }
      } else {
        err.netError = 1;
        reject(err);
      }
    });
  });
};

export const apiGetFetch = (url) => {
  return new Promise((resolve, reject) => {
    let u = url.startsWith("http") ? url : axios.defaults.baseURL + url;
    let access_token = methods.getOAuth2token();
    u = u.indexOf("?") > 0 ? `u&access_token=${access_token}` : `u?access_token=${access_token}`;

    api.ajax({
      url: u,
      method: "get",
      dataType: "json",
    }, (ret, err) => {
      if (ret) {
        if (ret.status) {
          resolve(ret.data);
        } else {
          ret.netError = 0;
          reject(ret);
        }
      } else {
        err.netError = 1;
        reject(err);
      }
      console.log("Querying finished!");
    });
  });
};


export const apiTokenFetch = (url, params)=>{
  return new Promise((resolve, reject) => {
    let u = url.startsWith("http") ? url : axios.defaults.baseURL + url;
    let {client_id,client_secret,grant_types:grant_type} = params;
    api.ajax({
      url: u,
      headers: {
        "Content-Type": "application/json;charset=utf-8",
      },
      method: "post",
      dataType: "json",
      data: {
        body: {
          client_id,
          client_secret,
          grant_type,
        },
      },
    }, (ret, err) => {
      if (ret) {
        if (ret.status) {
          resolve(ret.data);
        } else {
          ret.netError = 0;
          reject(ret);
        }
      } else {
        err.netError = 1;
        reject(err);
      }
    });
  });
};

export const apiLoginFetch = (url, params = {}) => {
  return new Promise((resolve, reject) => {

    let u = url.startsWith("http") ? url : axios.defaults.baseURL + url;

    api.ajax({
      url: u,
      headers: {
        "Content-Type": "application/json;charset=utf-8",
      },
      method: "post",
      dataType: "json",
      data: {
        body: {
          ...params,
        },
      },
    }, (ret, err) => {
      if (ret) {
        if (ret.status) {
          resolve(ret.data);
        } else {
          ret.netError = 0;
          reject(ret);
        }
      } else {
        err.netError = 1;
        reject(err);
      }
    });
  });
};

export const getFetch = async (url, params = {}) => {
  try {
    const res = await axios.get(url, params);
    return res;
  } catch (e) {
    return {
      code: -1,
      message: e.message,
      status: false,
    };
  }
};

export const postFetch = async (url, params = {}) => {
  try {
    const res = await axios.post(url, params);
    return res;
  } catch (e) {
    return {
      code: -1,
      message: e.message,
      status: false,
    };
  }
};


export default {
  apiGetFetch,
  apiPostFetch,
  apiLoginFetch,
};
